package com.example.myapplication1.loldemo.dinyview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


/**
 * Copyright (C), 2020, 重庆八进制有限公司
 *
 * @author: weng
 * @last-modifier: weng
 * @version: 1.0
 * @create-date: 2020/11/28 14:07
 * @last-modify-date: 2020/11/28 14:07
 * @description:嵌套滑动
 */
public class MyLinearLayout extends LinearLayout {

    private int mInitialTouchY;
    private int mLastTouchY;
    private int mTotalTouchY=0;

    public MyLinearLayout(Context context) {
        super(context);
    }

    public MyLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        View view = null;
        if (getChildCount() != 0) {
            for (int i = 0; i < getChildCount(); i++) {
                view = getChildAt(i);
                if (view instanceof RecyclerView) {
                    RecyclerView list = (RecyclerView) view;
                    int height = MeasureSpec.getSize(heightMeasureSpec);
                    int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
                    list.measure(widthMeasureSpec, heightSpec);
                }
            }
        }
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
        new Thread(){
            @Override
            public void run() {
                super.run();
            }
        }.start();
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {

//        拦截时间情况  1 向上滑动的时候，没有滑动到顶部 2、向下滑动 recycleview第一个位置显示，并且没有到达最初始的位置
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mInitialTouchY = (int) (event.getY() + 0.5f);
                Log.e("tag","onInterceptTouchEvent---"+"ACTION_DOWN");
//                return true;
                break;
            case MotionEvent.ACTION_MOVE:
                Log.e("tag","onInterceptTouchEvent---"+"ACTION_MOVE");

//                if (Math.abs(mTotalTouchY) > 300) {
//                    return super.onInterceptTouchEvent(event);
//                } else {
//                    return true;
//                }
                return true;
//                break;
            case MotionEvent.ACTION_UP:
                Log.e("tag","onInterceptTouchEvent---"+"ACTION_UP");
//                return true;

                break;

        }

        return super.onInterceptTouchEvent(event);
//        return  true;

    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        View view = null;
        RecyclerView list =null;
        if (getChildCount() != 0) {
            for (int i = 0; i < getChildCount(); i++) {
                view = getChildAt(i);
                if (view instanceof RecyclerView) {
                     list = (RecyclerView) view;
                    int pos = ((LinearLayoutManager) list.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
                }
            }
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:

                mInitialTouchY = (int) (event.getY() + 0.5f);
                Log.e("tag","onTouchEvent---"+"ACTION_UP");
                break;
            case MotionEvent.ACTION_MOVE:

                mLastTouchY = (int) (event.getY() + 0.5f);
                int dy = mLastTouchY - mInitialTouchY;
                Log.e("tag","dy--->"+dy);
                mTotalTouchY +=dy;
                mInitialTouchY = mLastTouchY;
                if (Math.abs(mTotalTouchY) < 300){
                    Log.e("tag","onTouchEvent---"+"ACTION_MOVE"+"--mTotalTouchY--->"+Math.abs(mTotalTouchY));

                    view.setY(view.getY() + dy);
                    return  super.onTouchEvent(event);
                }else {
                    return list.onTouchEvent(event);
                }

            case MotionEvent.ACTION_UP:

                Log.e("tag","onTouchEvent---"+"ACTION_UP");
                if (Math.abs(mTotalTouchY) < 300){
                    return  super.onTouchEvent(event);
                }else {
                    return list.onTouchEvent(event);
                }
//                break;
        }
        return super.onTouchEvent(event);
    }



}
